package com.sbw.wx.controller;

import com.alibaba.fastjson.JSONObject;
import com.sbw.wx.entity.deletemessage.DeleteMessageBean;
import com.sbw.wx.entity.graphicmessage.MpnewsType;
import com.sbw.wx.entity.graphicmessage.PreviewGraphicMessage;
import com.sbw.wx.entity.graphicmessage.TextType;
import com.sbw.wx.entity.groupsendmessage.*;
import com.sbw.wx.path.RequestPathConst;
import com.sbw.wx.utils.AccessTokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.text.MessageFormat;

/**
 * Desc: 图文消息控制器
 * @Date: 2021/6/27 21:35
 * @Author: Lee
 * 面朝大海  春暖花开
 * */
@Slf4j
@RestController
@RequestMapping("/graphicMessage")
public class GraphicMessageController {

    @Autowired
    private AccessTokenUtil accessTokenUtil;
    @Autowired
    private RestTemplate restTemplate;

    /**
     * Desc: 删除群发接口
     * @Date: 2021/6/27 22:57
     * @Author: Lee
     * 面朝大海  春暖花开
     * */
    @PostMapping("/deleteMessage")
    public String deleteMessage(@RequestBody DeleteMessageBean deleteMessageBean){
        return deleteMessage_(deleteMessageBean);
    }

    /**
     * Desc: 根据标签进行群发-图文消息 TODO 测试账号无权限，需要在正式账号中调试
     * @Date: 2021/6/27 18:45
     * @Author: Lee
     * 面朝大海  春暖花开
     * */
    @PostMapping("/sendGraphicMessageByTag")
    public String sendGraphicMessageByTag(@RequestBody GraphicTextMessageMediaByTag mediaInfo){
        return sendMessageByTag(mediaInfo);
    }

    /**
     * Desc: 根据标签进行群发-文本消息 TODO 测试账号无权限，需要在正式账号中调试
     * @Date: 2021/6/27 18:45
     * @Author: Lee
     * 面朝大海  春暖花开
     * */
    @PostMapping("/sendTextMessageByTag")
    public String sendTextMessageByTag(@RequestBody TextMediaByTag mediaInfo){
        return sendMessageByTag(mediaInfo);
    }

    /**
     * Desc: 根据OpenId进行群发-图文消息  TODO 测试账号无权限，需要在正式账号中调试
     * @Date: 2021/6/27 22:33
     * @Author: Lee
     * 面朝大海  春暖花开
     * */
    @PostMapping("/sendGraphicMessageByOpenId")
    public String sendGraphicMessageByOpenId(@RequestBody GraphicTextMessageMediaByOpenId mediaInfo){
        return sendMessageByOpenId(mediaInfo);
    }

    /**
     * Desc: 根据OpenId进行群发-文本消息  TODO 测试账号无权限，需要在正式账号中调试
     * @Date: 2021/6/27 22:34
     * @Author: Lee
     * 面朝大海  春暖花开
     * */
    @PostMapping("/sendTextMessageByOpenId")
    public String sendTextMessageOpenId(@RequestBody TextMediaByOpenId mediaInfo){
        return sendMessageByOpenId(mediaInfo);
    }

    /**
     * Desc: 预览图文消息
     * @Date: 2021/6/27 21:29
     * @Author: Lee
     * 面朝大海  春暖花开
     * */
    @PostMapping("/previewGraphicMessage")
    public String previewGraphicMessage(@RequestBody MpnewsType mpnewsType){
        return preview(mpnewsType);
    }

    /**
     * Desc: 预览文本消息
     * @Date: 2021/6/27 21:29
     * @Author: Lee
     * 面朝大海  春暖花开
     * */
    @PostMapping("/previewTextMessage")
    public String previewTextMessage(@RequestBody TextType textType){
        return preview(textType);
    }

    /**
     * Desc: 查询群发消息发送状态    TODO 群发消息接口不能调用，该接口不能测试
     * @Date: 2021/6/28 8:23
     * @Author: Lee
     * 面朝大海  春暖花开
     * */
    @PostMapping("/checkGroupSendStatus")
    public String checkGroupSendStatus(@RequestBody CheckGroupSendMessageBean messageBean){
        log.info("查询群发消息发送状态开始，方法入参:{}", messageBean.toString());
        String sendMessage2UserUrl = MessageFormat.format(RequestPathConst.checkGroupSendStatus, accessTokenUtil.getAccessToken());
        log.info("查询群发消息发送状态请求Url:{}", sendMessage2UserUrl);
        String requestJson = JSONObject.toJSONString(messageBean);
        log.info("查询群发消息发送状态请求参数:{}", requestJson);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> request = new HttpEntity(requestJson, headers);
        ResponseEntity<String> responseEntityStr = restTemplate.postForEntity(sendMessage2UserUrl, request, String.class);
        log.info("查询群发消息发送状态响应消息:{}", responseEntityStr.getBody());
        JSONObject retObj = JSONObject.parseObject(responseEntityStr.getBody());
        if (!StringUtils.isEmpty(retObj.getString("msg_id"))) {
            log.info("查询群发消息发送状态返回结果是:{}", retObj.toString());
            return retObj.toString();
        }
        log.error("查询群发消息发送状态异常，异常Code:{}, 异常信息:{}", retObj.get("errcode"), retObj.getString("errmsg"));
        return "";
    }

    private String deleteMessage_(DeleteMessageBean deleteMessageBean){
        log.info("删除群发消息开始，方法入参:{}", deleteMessageBean.toString());
        String sendMessage2UserUrl = MessageFormat.format(RequestPathConst.deleteGraphicMessage, accessTokenUtil.getAccessToken());
        log.info("删除群发消息请求Url:{}", sendMessage2UserUrl);
        String requestJson = JSONObject.toJSONString(deleteMessageBean);
        log.info("删除群发消息请求参数:{}", requestJson);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> request = new HttpEntity(requestJson, headers);
        ResponseEntity<String> responseEntityStr = restTemplate.postForEntity(sendMessage2UserUrl, request, String.class);
        log.info("删除群发消息响应消息:{}", responseEntityStr.getBody());
        JSONObject retObj = JSONObject.parseObject(responseEntityStr.getBody());
        if ("0".equals(retObj.getString("errcode"))) {
            log.info("删除群发消息返回结果是:{}", retObj.toString());
            return retObj.toString();
        }
        log.error("删除群发消息异常，异常Code:{}, 异常信息:{}", retObj.get("errcode"), retObj.getString("errmsg"));
        return "";
    }

    private String sendMessageByTag(AbstracByTag message){
        log.info("根据标签进行群发开始，方法入参:{}", message.toString());
        String sendMessage2UserUrl = MessageFormat.format(RequestPathConst.sendGraphicMessageByTag, accessTokenUtil.getAccessToken());
        log.info("根据标签进行群发，请求Url:{}", sendMessage2UserUrl);
        String requestJson = JSONObject.toJSONString(message);
        log.info("根据标签进行群发，请求参数:{}", requestJson);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> request = new HttpEntity(requestJson, headers);
        ResponseEntity<String> responseEntityStr = restTemplate.postForEntity(sendMessage2UserUrl, request, String.class);
        log.info("根据标签进行群发，响应消息:{}", responseEntityStr.getBody());
        JSONObject retObj = JSONObject.parseObject(responseEntityStr.getBody());
        if (StringUtils.isEmpty(retObj.getString("errcode"))) {
            log.info("根据标签进行群发结束，返回结果是:{}", retObj.toString());
            return retObj.toString();
        }
        log.error("根据标签进行群发异常，异常Code:{}, 异常信息:{}", retObj.get("errcode"), retObj.getString("errmsg"));
        return "";
    }

    private String sendMessageByOpenId(AbstracByOpenId message){
        log.info("根据OpenId进行群发开始，方法入参:{}", message.toString());
        String sendMessage2UserUrl = MessageFormat.format(RequestPathConst.sendGraphicMessageByOpenId, accessTokenUtil.getAccessToken());
        log.info("根据OpenId进行群发，请求Url:{}", sendMessage2UserUrl);
        String requestJson = JSONObject.toJSONString(message);
        log.info("根据OpenId进行群发，请求参数:{}", requestJson);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> request = new HttpEntity(requestJson, headers);
        ResponseEntity<String> responseEntityStr = restTemplate.postForEntity(sendMessage2UserUrl, request, String.class);
        log.info("根据OpenId进行群发，响应消息:{}", responseEntityStr.getBody());
        JSONObject retObj = JSONObject.parseObject(responseEntityStr.getBody());
        if (StringUtils.isEmpty(retObj.getString("errcode"))) {
            log.info("根据OpenId进行群发结束，返回结果是:{}", retObj.toString());
            return retObj.toString();
        }
        log.error("根据OpenId进行群发异常，异常Code:{}, 异常信息:{}", retObj.get("errcode"), retObj.getString("errmsg"));
        return "";
    }

    private String preview(PreviewGraphicMessage previewGraphicMessage){
        log.info("预览接口开始，方法入参:{}", previewGraphicMessage.toString());
        String previewGraphicMessageUrl = MessageFormat.format(RequestPathConst.previewGraphicMessage, accessTokenUtil.getAccessToken());
        log.info("预览接口请求Url:{}", previewGraphicMessageUrl);
        String requestJson = JSONObject.toJSONString(previewGraphicMessage);
        log.info("预览接口请求参数:{}", requestJson);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> request = new HttpEntity(requestJson, headers);
        ResponseEntity<String> responseEntityStr = restTemplate.postForEntity(previewGraphicMessageUrl, request, String.class);
        log.info("预览接口响应消息:{}", responseEntityStr.getBody());
        JSONObject retObj = JSONObject.parseObject(responseEntityStr.getBody());
        if ("0".equals(retObj.getString("errcode"))) {
            log.info("预览接口返回结果是:{}", retObj.toString());
            return retObj.toString();
        }
        log.error("预览接口异常，异常Code:{}, 异常信息:{}", retObj.get("errcode"), retObj.getString("errmsg"));
        return "";
    }

}
